<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>TypeScript 基础 | 学习笔记</title><meta name="author" content="lzoxun"><meta name="copyright" content="lzoxun"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="JavaScript 的超级，在js es6 es6以上的js语法这个添加了 接口(interfaces)、强类型(Strongly Typed)、泛型(Generics)…  安装1234567891011npm install -g typescript# 查看版本tsc -vtsc script.ts # 编译生成js文件tsc --init # 生成ts配置文件# vscode直接右键运行">
<meta property="og:type" content="article">
<meta property="og:title" content="TypeScript 基础">
<meta property="og:url" content="http://example.com/2023/07/22/web/web-script-ts/index.html">
<meta property="og:site_name" content="学习笔记">
<meta property="og:description" content="JavaScript 的超级，在js es6 es6以上的js语法这个添加了 接口(interfaces)、强类型(Strongly Typed)、泛型(Generics)…  安装1234567891011npm install -g typescript# 查看版本tsc -vtsc script.ts # 编译生成js文件tsc --init # 生成ts配置文件# vscode直接右键运行">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png">
<meta property="article:published_time" content="2023-07-22T07:14:09.698Z">
<meta property="article:modified_time" content="2023-07-22T07:14:09.698Z">
<meta property="article:author" content="lzoxun">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2023/07/22/web/web-script-ts/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":true,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'TypeScript 基础',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-07-22 15:14:09'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const now = new Date()
          const hour = now.getHours()
          const isNight = hour <= 6 || hour >= 18
          if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
          else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (false) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">108</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="学习笔记"><span class="site-name">学习笔记</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">TypeScript 基础</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-07-22T07:14:09.698Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-07-22T07:14:09.698Z" title="更新于 2023-07-22 15:14:09">2023-07-22</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="TypeScript 基础"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p><strong>JavaScript 的超级，在js es6 es6以上的js语法这个添加了 接口(interfaces)、强类型(Strongly Typed)、泛型(Generics)…</strong> </p>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">npm install -g typescript</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看版本</span></span><br><span class="line">tsc -v</span><br><span class="line">tsc script.ts # 编译生成js文件</span><br><span class="line">tsc --init # 生成ts配置文件</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">vscode直接右键运行ts文件</span></span><br><span class="line">npm install -g ts-node</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">ts-node 需要依赖  tslib @types/node 两个包</span></span><br><span class="line">npm install tslib @types/node -g</span><br><span class="line">ts-node script.ts # 直接运行 script.ts</span><br></pre></td></tr></table></figure>
<blockquote>
<p>  解决了JavaScript类型缺失问题，错误发现的越早越好，开发阶段对类型进行校验</p>
</blockquote>
<h2 id="编译"><a href="#编译" class="headerlink" title="编译"></a>编译</h2><h3 id="手动直接编译"><a href="#手动直接编译" class="headerlink" title="手动直接编译"></a>手动直接编译</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tsc file.ts</span><br></pre></td></tr></table></figure>

<h3 id="vscode自动编译"><a href="#vscode自动编译" class="headerlink" title="vscode自动编译"></a>vscode自动编译</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">1. 项目目录生成配置文件</span> </span><br><span class="line">tsc --init</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">2. 修改配置文件参数 如:</span></span><br><span class="line">&quot;strict&quot;: false </span><br><span class="line">&quot;outDir&quot;: &quot;./js&quot;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">3.启动监视任务</span></span><br><span class="line">终端 -&gt; 运行任务 -&gt; 监视xx项目 tsconfig.json</span><br><span class="line"></span><br><span class="line">或 </span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">tsc -p d:\lzo-project\lzo-everyday\tsconfig.json --watch 直接箭头指定的tsconfig</span></span><br></pre></td></tr></table></figure>
<h2 id="ts语法"><a href="#ts语法" class="headerlink" title="ts语法"></a>ts语法</h2><h3 id="类型注解"><a href="#类型注解" class="headerlink" title="类型注解"></a>类型注解</h3><blockquote>
<p> 这是一种轻量的为函数或者变量提添加的约束，冒号后面的都是类型</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 完整格式 var/let/const 标识符:数据类型 = 赋值</span></span><br><span class="line"><span class="comment">// 声明类型后，TypeScript 就会进行类型检测，声明类型称为 类型注解</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">age</span>: number = <span class="number">20</span>;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">tsfun</span>(<span class="params">str: string</span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(str);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//如果类型不对,编译时会报错</span></span><br><span class="line"><span class="title function_">tsfun</span>(<span class="string">&quot;str&quot;</span>);</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="基础数据类型"><a href="#基础数据类型" class="headerlink" title="基础数据类型"></a>基础数据类型</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 常用基础类型</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">a</span>: string = <span class="string">&quot;str&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> <span class="attr">b</span>: number = <span class="number">20</span>;</span><br><span class="line"><span class="keyword">let</span> <span class="attr">c</span>: boolean = <span class="literal">true</span>;</span><br><span class="line"><span class="keyword">let</span> <span class="attr">d</span>: string | number = <span class="number">20</span>; <span class="comment">//联合类型 </span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 对象,一般配合接口interface使用</span></span><br><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&quot;lzo&quot;</span>,</span><br><span class="line">  <span class="attr">age</span>: <span class="number">18</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// let obj: &#123;</span></span><br><span class="line"><span class="comment">//     name: string;</span></span><br><span class="line"><span class="comment">//     age: number;</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//非严格模式下（配置文件strict：false）undefined和null是可以赋值给其他类型变量的</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">e</span>: <span class="literal">null</span> = <span class="literal">null</span>;</span><br><span class="line"><span class="keyword">let</span> <span class="attr">f</span>: <span class="literal">undefined</span> = <span class="literal">undefined</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// js 不存在的类型</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">anytype</span>: any = <span class="string">&quot;any&quot;</span>; <span class="comment">// 值可以是任意类型，js的类型就是any</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">tuple</span>: [string, number] = [<span class="string">&quot;str&quot;</span>, <span class="number">20</span>]; <span class="comment">//元组：固定元素个数与元素类型</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">unknownType</span>: unknown = <span class="number">8</span>; <span class="comment">// 任意不确定类型，任意数据都是不合法的,只能在类型缩小后操作</span></span><br><span class="line">unknownType = <span class="string">&quot;12&quot;</span>; <span class="comment">// 赋值还是没问题的</span></span><br><span class="line"><span class="comment">// console.log(unknownType.length) // 这样的操作就不行了</span></span><br><span class="line"><span class="keyword">if</span> (<span class="keyword">typeof</span> unknownType == <span class="string">&quot;string&quot;</span>) &#123;</span><br><span class="line">  <span class="comment">// 必须进行类型校验(缩小 将类型缩小到一定范围)，才能根据缩小后的类型进行对应操作</span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(unknownType.<span class="property">length</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// void 表示函数没有返回值的时候，用void来表示</span></span><br><span class="line"><span class="comment">// 用户指定函数返回void的时候，允许函数不返回 和 返回undefined(返回null不行)</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">summ</span>(<span class="params">a: number, b: number</span>): <span class="keyword">void</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="literal">undefined</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 用户没有指定void的时候，如果返回undefined，返回值类型部分，类型推导为 undefined类型</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// never类型</span></span><br><span class="line"><span class="comment">// 函数不会返回任何东西时，报错或死循环，通常自动推导时可以出现</span></span><br><span class="line"><span class="comment">// 不可能有符合条件的地方，就会被推导为never （let aaa:number&amp;string;）aaa就是never类型</span></span><br><span class="line"><span class="comment">// switch 用于到不了的case或default里面的变量，（可能出现的case，但是没有处理，直接报错，让用户意识到少处理了一些）</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">foo</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">return</span> []</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">-----------------------------------------------------------------------</span><br><span class="line"></span><br><span class="line"><span class="comment">//枚举</span></span><br><span class="line">enum <span class="variable constant_">USER_ENUM</span> &#123;</span><br><span class="line">    <span class="variable constant_">USER</span>,</span><br><span class="line">    <span class="variable constant_">ADMIN</span>,</span><br><span class="line">    <span class="variable constant_">THREE</span> = <span class="string">&quot;three&quot;</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable constant_">USER_ENUM</span>.<span class="property">USER</span>); <span class="comment">//0</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable constant_">USER_ENUM</span>.<span class="property">ADMIN</span>); <span class="comment">//1</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable constant_">USER_ENUM</span>.<span class="property">THREE</span>); <span class="comment">//three</span></span><br><span class="line"><span class="comment">//可以通过数值类型值拿到key</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable constant_">USER_ENUM</span>[<span class="number">1</span>]); <span class="comment">//ADMIN</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//any 都可以类型</span></span><br><span class="line"><span class="keyword">const</span> <span class="attr">arr2</span>: any = [<span class="string">&quot;1&quot;</span>, <span class="number">2</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">//void类型像是与any类型相反,只能为它赋予undefined和null：</span></span><br><span class="line"><span class="keyword">const</span> <span class="attr">vi</span>: <span class="keyword">void</span> = <span class="literal">undefined</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//object 非基础数据类型</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">create</span> = (<span class="params">obj: object</span>) =&gt; &#123;&#125;;</span><br><span class="line"><span class="title function_">create</span>([]);</span><br><span class="line"><span class="title function_">create</span>(&#123;&#125;);</span><br><span class="line"><span class="title function_">create</span>(<span class="function">() =&gt;</span> &#123;&#125;);</span><br><span class="line"><span class="title function_">create</span>(<span class="title class_">String</span>);</span><br></pre></td></tr></table></figure>
<h4 id="类型别名-Type"><a href="#类型别名-Type" class="headerlink" title="类型别名 Type"></a>类型别名 Type</h4><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">MyNumber</span> = <span class="built_in">number</span></span><br><span class="line"><span class="keyword">const</span> <span class="attr">age</span>:<span class="title class_">MyNumber</span> = <span class="number">16</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 对象</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">PointType</span> = &#123;</span><br><span class="line">    <span class="attr">x</span>:<span class="built_in">number</span>  <span class="comment">// 逗号和封号都可以，如果有换行，可以不指定任何符号</span></span><br><span class="line">    <span class="attr">y</span>:<span class="built_in">number</span></span><br><span class="line">    z?:<span class="built_in">number</span> <span class="comment">// z可选</span></span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">add</span>(<span class="params">point:PointType</span>)&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(point.<span class="property">x</span>) <span class="comment">// 放心的使用point</span></span><br><span class="line">    <span class="keyword">return</span> point.<span class="property">x</span> + point.<span class="property">y</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">add</span>(&#123;<span class="attr">x</span>:<span class="number">1</span>,<span class="attr">y</span>:<span class="number">2</span>&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="类型推导"><a href="#类型推导" class="headerlink" title="类型推导"></a>类型推导</h4><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 类型推导/类型推断</span></span><br><span class="line"><span class="comment">// 声明标识符如果有赋值，会根据赋值的类型推导出标识符的类型注解</span></span><br><span class="line"><span class="keyword">let</span> lxtd = <span class="string">&quot;lxtd&quot;</span>;    <span class="comment">// 推导为 string</span></span><br><span class="line"><span class="keyword">let</span> lxtd2 = <span class="number">2.3</span>;      <span class="comment">// 推导为 number</span></span><br><span class="line"><span class="keyword">const</span> lxtd3 = <span class="number">2.3</span>;    <span class="comment">// 推导为 字面量类型2.3</span></span><br><span class="line"><span class="keyword">const</span> lxtd4 = <span class="string">&#x27;lxtd&#x27;</span>; <span class="comment">// 推导为 字面量类型lxtd</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> lxtd = <span class="string">&quot;str&quot;</span>;</span><br><span class="line">lxtd = <span class="number">124</span>; <span class="comment">//报错了 推断成了字符串类型</span></span><br></pre></td></tr></table></figure>

<h4 id="类型断言"><a href="#类型断言" class="headerlink" title="类型断言"></a>类型断言</h4><blockquote>
<p>  告诉ts要操作的对象就是那个类型，可以对这个对象做那个类型可以做的事情 </p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> imgEl = <span class="variable language_">document</span>.<span class="title function_">querySelector</span>(<span class="string">&quot;.img&quot;</span>) <span class="keyword">as</span> <span class="title class_">HTMLImageElement</span></span><br><span class="line">imgEl.<span class="property">src</span> = <span class="string">&#x27;xxx&#x27;</span> <span class="comment">// 如果没有断言，ts不知道 imgEl 就是 图片对象，给设置src就会报错</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//类型断言 </span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">getString</span>(<span class="params">str:<span class="built_in">string</span>|<span class="built_in">number</span></span>):<span class="built_in">number</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="attr">num</span>:<span class="built_in">number</span>;</span><br><span class="line">    <span class="comment">// 高速ts编译器我的str是什么类型，断言str为string就可以取length</span></span><br><span class="line">    <span class="comment">// 前提是str类型可以是string</span></span><br><span class="line">    <span class="comment">// 两种方式: &lt;类型&gt;变量  或  变量 as 类型</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// num = (&lt;string&gt;str).length</span></span><br><span class="line">    num = (str <span class="keyword">as</span> <span class="built_in">string</span>).<span class="property">length</span></span><br><span class="line">    <span class="keyword">return</span> num;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">getString</span>(<span class="number">12</span>))</span><br></pre></td></tr></table></figure>

<h4 id="非空类型断言"><a href="#非空类型断言" class="headerlink" title="非空类型断言 !"></a>非空类型断言 !</h4><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 非空类型断言</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">IPersion</span> &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="built_in">string</span>;</span><br><span class="line">  infos?: &#123;</span><br><span class="line">    <span class="attr">name</span>: <span class="built_in">string</span>;</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> <span class="attr">ematyInfo</span>: <span class="title class_">IPersion</span> = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&quot;lzo&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 访问时可以用 可选链?. 避免infos.name不存在时</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(ematyInfo.<span class="property">infos</span>?.<span class="property">name</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 赋值时就不能使用 ?. 了</span></span><br><span class="line"><span class="comment">// 方案1 利息缩小</span></span><br><span class="line"><span class="keyword">if</span>(ematyInfo.<span class="property">infos</span>)&#123;</span><br><span class="line">    ematyInfo.<span class="property">infos</span>.<span class="property">name</span> = <span class="string">&#x27;xun&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 方案2 非空类型断言（强制告诉ts infos一定不为空，危险如果断言错了，给undefined赋值，程序就出错了）</span></span><br><span class="line">ematyInfo.<span class="property">infos</span>!.<span class="property">name</span> = <span class="string">&#x27;xun&#x27;</span></span><br></pre></td></tr></table></figure>

<h4 id="字面量类型"><a href="#字面量类型" class="headerlink" title="字面量类型"></a>字面量类型</h4><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;lzo&#x27;</span> <span class="comment">// 具体值，const定义的变量，自动推导成字面量</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">age</span>:<span class="number">18</span> = <span class="number">18</span> <span class="comment">// 指定为字面量类型</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用场景</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">MethodType</span> = <span class="string">&quot;get&quot;</span> | <span class="string">&quot;post&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 1</span></span><br><span class="line"><span class="keyword">const</span> reqinfos = &#123;</span><br><span class="line">  <span class="attr">method</span>: <span class="string">&quot;post&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 2</span></span><br><span class="line"><span class="keyword">const</span> reqinfos2 = &#123;</span><br><span class="line">  <span class="attr">method</span>: <span class="string">&quot;post&quot;</span></span><br><span class="line">&#125; <span class="keyword">as</span> <span class="keyword">const</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 3</span></span><br><span class="line"><span class="keyword">interface</span> reqinfos3Inter &#123;</span><br><span class="line">  <span class="attr">method</span>: <span class="title class_">MethodType</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> <span class="attr">reqinfos3</span>: reqinfos3Inter = &#123;</span><br><span class="line">  <span class="attr">method</span>: <span class="string">&quot;post&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">requet</span>(<span class="params">url: <span class="built_in">string</span>, method: MethodType</span>) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(method);</span><br><span class="line">&#125; <span class="comment">// 使用 MethodType 比 string 更加合理</span></span><br><span class="line"></span><br><span class="line"><span class="title function_">requet</span>(<span class="string">&quot;url&quot;</span>, <span class="string">&quot;post&quot;</span>); <span class="comment">// 可以</span></span><br><span class="line"><span class="title function_">requet</span>(<span class="string">&quot;url&quot;</span>, reqinfos.<span class="property">method</span> <span class="keyword">as</span> <span class="title class_">MethodType</span>); <span class="comment">// reqinfos.method 是 string 类型 不能直接赋值到 MethodType 类型</span></span><br><span class="line"><span class="title function_">requet</span>(<span class="string">&quot;url&quot;</span>, reqinfos2.<span class="property">method</span>); <span class="comment">// 直接将 reqinfos2 所有属性变成字面量类型</span></span><br><span class="line"><span class="title function_">requet</span>(<span class="string">&quot;url&quot;</span>, reqinfos3.<span class="property">method</span>); <span class="comment">// 直接将 reqinfos3.method 变成字面量类型 </span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="函数类型"><a href="#函数类型" class="headerlink" title="函数类型"></a>函数类型</h4><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 函数类型（类型返回值用的是箭头）</span></span><br><span class="line"><span class="comment">// 有些语言这个参数名称 a|b 是可以省略的，ts中是不可省略的，如果省略就变成 string:any </span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">add</span>: <span class="function">(<span class="params">a: <span class="built_in">string</span>, b: <span class="built_in">string</span></span>) =&gt;</span> <span class="built_in">string</span> = (<span class="attr">a</span>: <span class="built_in">string</span>, <span class="attr">b</span>: <span class="built_in">string</span>): <span class="function"><span class="params">string</span> =&gt;</span> a + b;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">add</span>(<span class="string">&quot;1&quot;</span>, <span class="string">&quot;211&quot;</span>));                                      </span><br><span class="line"> </span><br><span class="line"><span class="comment">// 提取</span></span><br><span class="line"><span class="keyword">type</span> add2Type = <span class="function">(<span class="params">a: <span class="built_in">string</span>, b: <span class="built_in">string</span></span>) =&gt;</span> <span class="built_in">string</span>; <span class="comment">// 指定返回值必须为string</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">add2</span>: add2Type = (<span class="attr">a</span>: <span class="built_in">string</span>, <span class="attr">b</span>: <span class="built_in">string</span>): <span class="function"><span class="params">string</span> =&gt;</span> a + b;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">add</span>(<span class="string">&quot;1&quot;</span>, <span class="string">&quot;211&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 根据执行上下文自动推导匿名函数的类型</span></span><br><span class="line"><span class="keyword">let</span> arr5 = [<span class="string">&quot;1&quot;</span>, <span class="string">&quot;2&quot;</span>, <span class="number">3</span>];</span><br><span class="line">arr5.<span class="title function_">forEach</span>(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;&#125;);</span><br></pre></td></tr></table></figure>

<h4 id="交叉类型"><a href="#交叉类型" class="headerlink" title="交叉类型"></a>交叉类型</h4><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 交叉类型 （多种类型同时满足）</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">Info</span> &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="built_in">string</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">Info2</span> &#123;</span><br><span class="line">  <span class="attr">age</span>: <span class="built_in">number</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> <span class="attr">infos</span>: <span class="title class_">Info</span> &amp; <span class="title class_">Info2</span> = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&quot;lzo&quot;</span>,</span><br><span class="line">  <span class="attr">age</span>: <span class="number">18</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 数组</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">arr</span>: <span class="built_in">number</span>[] = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]; <span class="comment">//数组 指定数据类型，只能放数字</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">arr2</span>: (<span class="built_in">string</span> | <span class="built_in">number</span>)[] = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">&quot;str&quot;</span>]; <span class="comment">//数组 指定数据类型,可存放多种类型</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">list</span>: <span class="title class_">Array</span>&lt;<span class="built_in">number</span>&gt; = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]; <span class="comment">// 泛型写法</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">list2</span>: <span class="title class_">ReadonlyArray</span>&lt;<span class="built_in">number</span>&gt; = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]; <span class="comment">//list2的元素确保不会被修改</span></span><br></pre></td></tr></table></figure>

<h4 id="类型缩小（类型保护）"><a href="#类型缩小（类型保护）" class="headerlink" title="类型缩小（类型保护）"></a>类型缩小（类型保护）</h4><blockquote>
<p>  将需要使用的变量类型范围进行缩小</p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 联合类型案例</span></span><br><span class="line"><span class="comment">// typeof</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">val</span>:<span class="built_in">number</span>|<span class="built_in">string</span> = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">if</span>(<span class="keyword">typeof</span> val == <span class="built_in">string</span>)&#123; <span class="comment">// val 的类型可以是 number 和 string，这里缩小为string，直接就能使用string的方法</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(val.<span class="property">length</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// === or !==</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">Direction</span> = <span class="string">&quot;left&quot;</span> | <span class="string">&quot;right&quot;</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">switchdir</span>(<span class="params">direction:Direction</span>)&#123;</span><br><span class="line">    <span class="keyword">if</span>(direction === <span class="string">&#x27;left&#x27;</span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;这是left&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// instanceof</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">date</span>:<span class="built_in">string</span>|<span class="title class_">Date</span> = <span class="string">&quot;123&quot;</span></span><br><span class="line"><span class="keyword">if</span>(date <span class="keyword">instanceof</span> <span class="title class_">Date</span>)&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(date.<span class="title function_">getTime</span>())</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// in 判断是否有某个属性  （&quot;name&quot; in obj 判断obj对象是否存在name属性）</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// ....</span></span><br></pre></td></tr></table></figure>



<p>总结</p>
<ul>
<li><code>number</code>、<code>string</code>、<code>boolean</code>、<code>null</code>、<code>undefined</code>、数组<code>number[]</code>、元祖<code>[string, number,xxx]</code>、枚举<code>enum</code>、<code>any</code>、<code>void</code>、<code>object</code>等十几类</li>
<li>联合类型： number | string</li>
<li>类型断言： <code>&lt;类型&gt;变量</code>  或  <code>变量 as 类</code>，两种方式让编译器把变量当做指定的类型操作</li>
<li>类型推断:  定义变量时<code>没有指定类型</code>,编译器会把根据变量的值推断出一个类型,没值就是any</li>
</ul>
<h3 id="interface-与-对象"><a href="#interface-与-对象" class="headerlink" title="interface 与 对象"></a>interface 与 对象</h3><blockquote>
<p>接口是一种能力或一种约束</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * type 与 interface 的区别 </span></span><br><span class="line"><span class="comment"> *      1、type 对类型使用范围广，interface 只能声明对象（但对对象使用的范围更广）</span></span><br><span class="line"><span class="comment"> *      2、声明对象时 interface 相同名称可以多次声明，属性叠加（使用别接口，扩展自己需要加入的东西），type不能声明两个相同名称的别名同时存在</span></span><br><span class="line"><span class="comment"> *      3、interface 支持继承 interface Persion2 extends Person &#123;加入Persion2 独有的属性&#125; </span></span><br><span class="line"><span class="comment"> *      4、interface 可以被类实现 class Tree implements Person （Person 中所有属性方法都要被实现）</span></span><br><span class="line"><span class="comment"> *      5、总结建议：非对象类型使用type，如果使用对象类型的声明使用interface</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line">----------------------------------------------------------------------------------------------</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义一个接口,如果类型不对||使用没有定义的属性||obj属性不够,ts会提示错误</span></span><br><span class="line">interface <span class="title class_">Person</span> &#123;</span><br><span class="line">    <span class="attr">firsName</span>: string;</span><br><span class="line">    <span class="attr">lastName</span>: string;</span><br><span class="line">    <span class="attr">age</span>: number;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//我们传入的对象参数实际上会包含很多属性，但是编译器只会检查那些必需的属性是否存在，并且其类型是否匹配</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">showName</span>(<span class="params">person: Person</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> person.<span class="property">firsName</span> + person.<span class="property">lastName</span> + <span class="string">&quot;,&quot;</span> + person.<span class="property">age</span> + <span class="string">&quot;岁&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">    <span class="attr">firsName</span>: <span class="string">&quot;liao&quot;</span>,</span><br><span class="line">    <span class="attr">lastName</span>: <span class="string">&quot;zhongxun&quot;</span>,</span><br><span class="line">    <span class="attr">age</span>: <span class="number">22</span>,</span><br><span class="line">    <span class="attr">xxx</span>:<span class="string">&#x27;xxx&#x27;</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="title function_">showName</span>(obj);</span><br><span class="line"></span><br><span class="line"><span class="comment">//====================================</span></span><br><span class="line"><span class="comment">//定义一个接口,用来描述对象形状的interface类型</span></span><br><span class="line">interface <span class="title class_">Person</span> &#123;</span><br><span class="line">    readonly <span class="attr">firsName</span>: string; <span class="comment">//名称定义好后不能随意更改</span></span><br><span class="line">    <span class="attr">lastName</span>: string;</span><br><span class="line">    age?: number; <span class="comment">//可填可不填</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> <span class="attr">per1</span>: <span class="title class_">Person</span> = &#123;</span><br><span class="line">    <span class="attr">firsName</span>: <span class="string">&quot;liao&quot;</span>,</span><br><span class="line">    <span class="attr">lastName</span>: <span class="string">&quot;zhongxun&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//实例类型断言,使Person中不存在的key属性不会报错</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">per2</span>: <span class="title class_">Person</span> = &#123;</span><br><span class="line">    ...per1,</span><br><span class="line">    <span class="attr">key</span>: [<span class="string">&quot;xxx&quot;</span>, <span class="number">12</span>],</span><br><span class="line">&#125; <span class="keyword">as</span> <span class="title class_">Person</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//接口扩展,继承</span></span><br><span class="line">interface <span class="title class_">Persion2</span> <span class="keyword">extends</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">    <span class="attr">type</span>: string;</span><br><span class="line">    [<span class="attr">key</span>: string]: any; <span class="comment">//定义接口设置，任意类型,创建对象时使Persion2中不存在的key属性不会报错</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> <span class="attr">per3</span>: <span class="title class_">Persion2</span> = &#123;</span><br><span class="line">    ...per1,</span><br><span class="line">    <span class="attr">type</span>: <span class="string">&quot;extend&quot;</span>,</span><br><span class="line">    <span class="attr">a</span>: <span class="string">&quot;2&quot;</span>,</span><br><span class="line">    <span class="attr">c</span>: [<span class="string">&quot;xxx&quot;</span>, <span class="number">12</span>],</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//======================================================================</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//函数签名</span></span><br><span class="line">interface <span class="title class_">ISearchFunc</span> &#123;</span><br><span class="line">    <span class="comment">//定义一个调用签名</span></span><br><span class="line">    (<span class="attr">a</span>: string, <span class="attr">b</span>: string): boolean;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义一个函数，使用定义的签名</span></span><br><span class="line"><span class="keyword">const</span> <span class="attr">searchFunc</span>: <span class="title class_">ISearchFunc</span> = (<span class="attr">a</span>: string, <span class="attr">b</span>: string): <span class="function"><span class="params">boolean</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> a.<span class="title function_">search</span>(b) &gt; -<span class="number">1</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">searchFunc</span>(<span class="string">&quot;123&quot;</span>, <span class="string">&quot;123&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//======================================================================</span></span><br><span class="line"><span class="comment">//函数签名</span></span><br><span class="line">interface <span class="title class_">ISearchFunc</span> &#123;</span><br><span class="line">    <span class="comment">//定义一个调用签名</span></span><br><span class="line">    (<span class="attr">a</span>: string, <span class="attr">b</span>: string): boolean;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//类接口</span></span><br><span class="line">interface <span class="title class_">IFly</span>&#123;</span><br><span class="line">    <span class="attr">fly</span>:<span class="title class_">ISearchFunc</span></span><br><span class="line">&#125;</span><br><span class="line">interface <span class="title class_">IFly2</span>&#123;</span><br><span class="line">    <span class="attr">fly2</span>:<span class="title class_">ISearchFunc</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//类中 implements 的类接口都要实现</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span> implements <span class="title class_">IFly</span>,<span class="title class_">IFly2</span>&#123;</span><br><span class="line">    <span class="title function_">fly</span>(<span class="attr">a</span>: string, <span class="attr">b</span>: string): boolean &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;实现IFly里的定义&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">fly2</span>(<span class="attr">a</span>: string, <span class="attr">b</span>: string): boolean &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;实现IFly2里的定义&quot;</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> per1 = <span class="keyword">new</span> <span class="title class_">Person</span>()</span><br><span class="line">per1.<span class="title function_">fly</span>(<span class="string">&quot;1&quot;</span>,<span class="string">&quot;2&quot;</span>)</span><br><span class="line">per1.<span class="title function_">fly2</span>(<span class="string">&quot;3&quot;</span>,<span class="string">&quot;4&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h3><blockquote>
<p>格式 test(变量,变量): 返回值{}   </p>
</blockquote>
<h4 id="函数完整写法"><a href="#函数完整写法" class="headerlink" title="函数完整写法"></a>函数完整写法</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *  add变量:函数名</span></span><br><span class="line"><span class="comment"> *  (a: string, b: string) =&gt; string ==== add函数的类型</span></span><br><span class="line"><span class="comment"> *  (a: string, b: string): string =&gt; a + b ==== 符合这个类型的函数</span></span><br><span class="line"><span class="comment"> *  最后面的 string 指定返回值的类型，写上去方便使用的人知道，得到的是什么样的值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">add</span>: <span class="function">(<span class="params">a: string, b: string</span>) =&gt;</span> string = (<span class="attr">a</span>: string, <span class="attr">b</span>: string): <span class="function"><span class="params">string</span> =&gt;</span> a + b;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 提取</span></span><br><span class="line">type add2Type = <span class="function">(<span class="params">a: string, b: string</span>) =&gt;</span> string; <span class="comment">// 指定返回值必须为string</span></span><br><span class="line"><span class="keyword">let</span> <span class="attr">add2</span>: add2Type = (<span class="attr">a</span>: string, <span class="attr">b</span>: string): <span class="function"><span class="params">string</span> =&gt;</span> a + b;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">add</span>(<span class="string">&quot;1&quot;</span>, <span class="string">&quot;211&quot;</span>));</span><br></pre></td></tr></table></figure>

<h4 id="参数"><a href="#参数" class="headerlink" title="参数"></a>参数</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> a  默认参数,有默认值,可以不传</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> b  可选参数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> ...args  剩余参数,必须放在最后面,如果参数够多，默认和可选一定会占一个</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">test</span>(<span class="params">a: number = <span class="number">123</span>, b: number,...args:number[]</span>): number &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(a) <span class="comment">//1</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(b) <span class="comment">//2</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(args) <span class="comment">//[3,4,5,6,7,8]</span></span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">test</span>(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>)</span><br></pre></td></tr></table></figure>
<h4 id="参数个数问题"><a href="#参数个数问题" class="headerlink" title="参数个数问题"></a>参数个数问题</h4><blockquote>
<p>  TypeScript 对于传入的函数类型的参数个数，少于应有的个数是，不进行检测数量的（但会检测类型） </p>
<p>  因为js提供很多方法，有很多参数，有很多不常用的</p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line">arr.<span class="title function_">forEach</span>(<span class="function">(<span class="params">item,index</span>)=&gt;</span>&#123;&#125;) <span class="comment">// 比如这里，其实还有第三个参数 array数组本生，一般情况基本不会用，没必要强制写入</span></span><br></pre></td></tr></table></figure>

<h4 id="函数调用签名"><a href="#函数调用签名" class="headerlink" title="函数调用签名"></a>函数调用签名</h4><blockquote>
<p>  调用签名（Call Signatures），函数除了可以被调用，<strong>自己也</strong>可以<strong>拥有属性值</strong></p>
</blockquote>
<blockquote>
<p>  函数表达式并不支持声明属性，如果需要描述一个带属性的函数，就需要在对象类型中写一个<strong>调用签名</strong>了</p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 函数调用签名</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">ISearchFunc</span> &#123;</span><br><span class="line">  <span class="comment">//定义一个调用签名</span></span><br><span class="line">  (<span class="attr">a</span>: <span class="built_in">string</span>, <span class="attr">b</span>: <span class="built_in">string</span>): <span class="built_in">boolean</span>; <span class="comment">// 这边是冒号，前面的函数类型用的是 箭头=&gt;</span></span><br><span class="line">  <span class="attr">name</span>: <span class="built_in">string</span>; <span class="comment">// 添加属性</span></span><br><span class="line">  <span class="attr">age</span>: <span class="built_in">number</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="attr">bar</span>: <span class="title class_">ISearchFunc</span> = <span class="function">(<span class="params">a: <span class="built_in">string</span>, b: <span class="built_in">string</span></span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;;</span><br><span class="line">bar.<span class="property">name</span> = <span class="string">&quot;lzo&quot;</span>;</span><br><span class="line">bar.<span class="property">age</span> = <span class="number">18</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="构造签名-了解"><a href="#构造签名-了解" class="headerlink" title="构造签名(了解)"></a>构造签名(了解)</h4><blockquote>
<p>  描述某个函数式当做构造函数使用的</p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span> &#123;&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">IPerson</span> &#123;</span><br><span class="line">    <span class="keyword">new</span> (): <span class="title class_">Person</span>     </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">factory</span>(<span class="params">fn:IPerson</span>)&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">123</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">factory</span>(<span class="title class_">Date</span>))</span><br></pre></td></tr></table></figure>

<h4 id="可选-默认-剩余参数"><a href="#可选-默认-剩余参数" class="headerlink" title="可选&#x2F;默认&#x2F;剩余参数"></a>可选&#x2F;默认&#x2F;剩余参数</h4><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 可选</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">bar</span>(<span class="params">x:<span class="built_in">number</span>,y?:<span class="built_in">number</span></span>)&#123; <span class="comment">// 可选参数y的类型为 number|undefined 的联合类型</span></span><br><span class="line">    <span class="comment">// 参数y需要缩小才能使用 </span></span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">bar</span>(<span class="number">10</span>)</span><br><span class="line"><span class="title function_">bar</span>(<span class="number">10</span>,<span class="number">20</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 默认</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">bar2</span>(<span class="params">x:<span class="built_in">number</span>,y = <span class="number">100</span></span>)&#123;&#125; <span class="comment">// 类型注解可以省略</span></span><br><span class="line"><span class="title function_">bar</span>(<span class="number">10</span>)</span><br><span class="line"><span class="title function_">bar</span>(<span class="number">10</span>,<span class="literal">undefined</span>) <span class="comment">// 可以接收undefined值，undefined和不传是一样的，内部发现是undefined才使用默认值100</span></span><br><span class="line"><span class="title function_">bar</span>(<span class="number">10</span>,<span class="number">20</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 剩余 </span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">bar3</span>(<span class="params">...args:<span class="built_in">number</span>[]</span>)&#123;&#125;</span><br><span class="line"><span class="title function_">bar3</span>(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>)</span><br></pre></td></tr></table></figure>

<h4 id="重载-了解"><a href="#重载-了解" class="headerlink" title="重载(了解)"></a>重载(了解)</h4><blockquote>
<p>函数名字相同，但是参数类型或个数不同</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">getInfo</span>(<span class="params">name:string</span>):<span class="keyword">void</span>; <span class="comment">// 重载签名</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">getInfo</span>(<span class="params">age:number</span>):<span class="keyword">void</span>;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">getInfo</span>(<span class="params">str:any</span>):<span class="keyword">void</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> str == <span class="string">&quot;string&quot;</span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;名字:&quot;</span>,str)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> str == <span class="string">&quot;number&quot;</span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;年龄&quot;</span>,str)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">getInfo</span>(<span class="string">&quot;zhangsan&quot;</span>)</span><br></pre></td></tr></table></figure>

<h4 id="函数中的this类型"><a href="#函数中的this类型" class="headerlink" title="函数中的this类型"></a>函数中的this类型</h4><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> thobj = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&quot;lzo&quot;</span>,</span><br><span class="line">  <span class="attr">fn</span>: <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 没有ts配置文件时，默认 any 类型不报错</span></span><br><span class="line">    <span class="comment">// 属性 noImplicitThis:true 根据上下文推导出this是谁</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">name</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">thobj2</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="comment">//console.log(this) // noImplicitThis:true  这里就不行了，不能有模糊类型的this存在，需要明确指定</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">thobj3</span>(<span class="params"><span class="variable language_">this</span>: &#123; name: <span class="built_in">string</span> &#125;, info: &#123; name: <span class="built_in">string</span> &#125;</span>) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>); <span class="comment">// 同过第一个参数指定this</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">thobj.<span class="title function_">fn</span>();</span><br><span class="line">thobj3.<span class="title function_">call</span>(&#123; <span class="attr">name</span>: <span class="string">&quot;lzo&quot;</span> &#125;, &#123; <span class="attr">name</span>: <span class="string">&quot;lzo&quot;</span> &#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// ts还有很多this内置工具省略</span></span><br></pre></td></tr></table></figure>



<h4 id="匿名函数"><a href="#匿名函数" class="headerlink" title="匿名函数"></a>匿名函数</h4><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 匿名函数最好不谣添加类型注解,例子中根据执行上下文推导出具体类型</span></span><br><span class="line"><span class="keyword">let</span> arr = [<span class="string">&quot;1&quot;</span>,<span class="string">&quot;2&quot;</span>]</span><br><span class="line">arr.<span class="title function_">forEach</span>(<span class="function">(<span class="params">item,index</span>)=&gt;</span>&#123;&#125;) <span class="comment">// item:string,index:number</span></span><br></pre></td></tr></table></figure>

<h4 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//函数 参数、返回值</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">test</span>(<span class="params">a: string=<span class="string">&quot;123&quot;</span>, b: string</span>): string &#123;</span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//声明一个类型:一般在定义联合类型或定义临时变量时可以使用</span></span><br><span class="line">type <span class="title class_">Sum</span> = <span class="function">(<span class="params">a: string, b: string</span>) =&gt;</span> string;</span><br><span class="line"></span><br><span class="line"><span class="comment">//interface 声明类型可以继承,可以被类来实现</span></span><br><span class="line"><span class="comment">// interface Sum &#123;</span></span><br><span class="line"><span class="comment">//     (a: string, b: string): string;</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="attr">test2</span>: <span class="title class_">Sum</span> = (<span class="attr">a</span>: string, <span class="attr">b</span>: string): <span class="function"><span class="params">string</span> =&gt;</span> a + b;</span><br><span class="line"><span class="comment">//test2类型 (a: string, b: string) =&gt;string,vscode鼠标移入会自动推导出来</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="泛型"><a href="#泛型" class="headerlink" title="泛型"></a>泛型</h3><blockquote>
<p>泛型，在代码执行是传入类型，来确定结果  </p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *  泛型:</span></span><br><span class="line"><span class="comment">     *      在定义接口、函数、类的时候不能确定要使用的数据类型，只能在接口、函数、类调用的时候才能确定的数据类型</span></span><br><span class="line"><span class="comment">     *       定义时不知道类型，利用T..等字幕占位，调用的时候将类型传入</span></span><br><span class="line"><span class="comment">     *  T:type</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> createArray&lt;T&gt;(<span class="attr">value</span>: T): T[] &#123;</span><br><span class="line">        <span class="keyword">return</span> [value];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//指定T为number类型number</span></span><br><span class="line">    <span class="keyword">let</span> caarr1 = createArray&lt;number&gt;(<span class="number">1</span>);</span><br><span class="line">    <span class="comment">//一个泛型,调用时如果不指定，编译器通过参数自动推导出T的类型</span></span><br><span class="line">    <span class="keyword">let</span> caarr2 = <span class="title function_">createArray</span>(<span class="string">&quot;1&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//----------------------------------------------------------------------</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> createArray2&lt;T, K&gt;(<span class="attr">value</span>: T, <span class="attr">key</span>: K): K[] &#123;</span><br><span class="line">        <span class="keyword">return</span> [key];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> ca1 = createArray2&lt;string, number&gt;(<span class="string">&quot;123&quot;</span>, <span class="number">123</span>);</span><br><span class="line">    <span class="keyword">let</span> ca2 = <span class="title function_">createArray2</span>(<span class="string">&quot;123&quot;</span>, <span class="literal">true</span>); <span class="comment">//通过参数自动推导出T,K的类型</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//----------------------------------------------------------------------</span></span><br><span class="line">    <span class="comment">//多个泛型实现反转</span></span><br><span class="line">    <span class="keyword">let</span> swap = &lt;T, K&gt;(<span class="attr">tuple</span>: [T, K]): [K, T] =&gt; &#123;</span><br><span class="line">        <span class="keyword">return</span> [tuple[<span class="number">1</span>], tuple[<span class="number">0</span>]];</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">swap</span>([<span class="string">&quot;123&quot;</span>,<span class="literal">true</span>]))</span><br><span class="line"></span><br><span class="line">    <span class="comment">//-----类中----------------------------------------------------</span></span><br><span class="line">    <span class="keyword">class</span> <span class="title class_">Person</span>&lt;T&gt; &#123;</span><br><span class="line">        <span class="attr">name</span>: T;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">new</span> <span class="title class_">Person</span>&lt;string&gt;()</span><br><span class="line"></span><br><span class="line">    <span class="comment">//-----接口中--------------------------------------------</span></span><br><span class="line">    interface <span class="title class_">PersonInt</span> &lt;T&gt;&#123;</span><br><span class="line">        <span class="attr">name</span>:T</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> <span class="attr">pint</span>:<span class="title class_">PersonInt</span>&lt;string&gt; = &#123;</span><br><span class="line">        <span class="attr">name</span>:<span class="string">&quot;123&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(pint)</span><br><span class="line">&#125;)();</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="泛型约束"><a href="#泛型约束" class="headerlink" title="泛型约束"></a>泛型约束</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *  泛型约束:</span></span><br><span class="line"><span class="comment">     *      如果直接对一个泛型取length会报错，一个编译器不知道T到的有没有length</span></span><br><span class="line"><span class="comment">     *      限制调用时传入的内容</span></span><br><span class="line"><span class="comment">     *      &lt;T extends ILength&gt; //必须有长度,并且可调用split方法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//定义一个接口用来约束将来某个类型中必要的length属性</span></span><br><span class="line">    interface <span class="title class_">ILength</span> &#123;</span><br><span class="line">        <span class="attr">length</span>: number;</span><br><span class="line">        split</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> getLength&lt;T <span class="keyword">extends</span> <span class="title class_">ILength</span>&gt;(<span class="attr">x</span>: T): number &#123;</span><br><span class="line">        x.<span class="title function_">split</span>(<span class="string">&quot;&quot;</span>)</span><br><span class="line">        <span class="keyword">return</span> x.<span class="property">length</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(getLength&lt;string&gt;(<span class="string">&quot;ds&quot;</span>))</span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>

<h3 id="类"><a href="#类" class="headerlink" title="类"></a>类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * class</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 继承:类与类的关系</span></span><br><span class="line"><span class="comment"> * 继承后类与类之间的叫法:</span></span><br><span class="line"><span class="comment"> *      A类继承B类:那么A类叫子类，B类叫基类</span></span><br><span class="line"><span class="comment"> *           子类: -&gt; 派生类 </span></span><br><span class="line"><span class="comment"> *           基类: -&gt; 超类（父类）</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * 多态:子类重写父类结果 是不同子类实例调用父级可以产生不同的结果</span></span><br><span class="line"><span class="comment"> * 修饰符:作用类中的成员,拿来设置成员(属性、构造函数、方法)的可访问性</span></span><br><span class="line"><span class="comment"> *      类中成员有默认修饰符public(外部实例可以通过 . 访问类中成员)</span></span><br><span class="line"><span class="comment"> *      public:公共的，任何地方都能访问</span></span><br><span class="line"><span class="comment"> *      protected:受保护的，(修饰后类类外部无法访问,但子类可以访问)</span></span><br><span class="line"><span class="comment"> *      private:私有的,(修饰后类类外部与子类都无法访问)</span></span><br><span class="line"><span class="comment"> *      !readonly:只读的,对类中的 属性 成进行修饰，修饰后其他地方只能获取，只有构造函数中能修改</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * 四种修饰符修饰构造函数的属性：</span></span><br><span class="line"><span class="comment"> *      效果一样</span></span><br><span class="line"><span class="comment"> *      但是有修饰符的属性就不需要定义成员属性，也不需要this.name = name;的步骤了</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> *  。。。。</span></span><br><span class="line"><span class="comment"> *  https://www.bilibili.com/video/BV1rf4y167am?p=26&amp;spm_id_from=pageDriver</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">;(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">//定义一个基类</span></span><br><span class="line">    <span class="keyword">class</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">        <span class="comment">//定义属性</span></span><br><span class="line">        <span class="attr">name</span>: string;</span><br><span class="line">        <span class="attr">age</span>: number;</span><br><span class="line">        <span class="attr">gender</span>: string; <span class="comment">//性别</span></span><br><span class="line">        <span class="attr">sum</span>: any;</span><br><span class="line">        <span class="comment">//定义构造函数</span></span><br><span class="line">        <span class="title function_">constructor</span>(<span class="params">name: string, age: number, gender: string</span>) &#123;</span><br><span class="line">            <span class="comment">//更新属性</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">name</span> = name;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">age</span> = age;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">gender</span> = gender;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">sum</span> = <span class="function">(<span class="params">str:string</span>) =&gt;</span> &#123; <span class="comment">// 构造函数中的实例方法，每new一次都会创建</span></span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(str);</span><br><span class="line">            &#125;;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//定义实例方法  prototype 的方法</span></span><br><span class="line">        <span class="title function_">sayHi</span>(<span class="params">str: string</span>) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`我是<span class="subst">$&#123;<span class="variable language_">this</span>.name&#125;</span>`</span>, str);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 静态方法</span></span><br><span class="line">        <span class="keyword">static</span> <span class="title function_">staticTest</span>(<span class="params">str: string</span>) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;金泰&quot;</span> + str);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//定义一个子类</span></span><br><span class="line">    <span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Person</span>&#123;</span><br><span class="line">        <span class="comment">//定义构造函数</span></span><br><span class="line">        <span class="title function_">constructor</span>(<span class="params">name: string, age: number, gender: string</span>) &#123;</span><br><span class="line">            <span class="comment">//使用super调用父级的构造函数</span></span><br><span class="line">            <span class="variable language_">super</span>(name,age,gender);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="title function_">sayHi</span>(<span class="params"></span>)&#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`我是子类重写sayHi`</span>)</span><br><span class="line">            <span class="variable language_">super</span>.<span class="title function_">sayHi</span>(<span class="string">&quot;我在调用父类的sayHi方法&quot;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">let</span> stu1 = <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;aa&quot;</span>,<span class="number">10</span>,<span class="string">&#x27;男&#x27;</span>);</span><br><span class="line">    stu1.<span class="title function_">sayHi</span>();</span><br><span class="line">    <span class="title class_">Student</span>.<span class="title function_">staticTest</span>(<span class="string">&quot;Student&quot;</span>);</span><br><span class="line">&#125;)();</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="存取器"><a href="#存取器" class="headerlink" title="存取器"></a>存取器</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 存取器</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">;(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">//定义一个基类</span></span><br><span class="line">    <span class="keyword">class</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">        <span class="comment">//定义属性</span></span><br><span class="line">        <span class="attr">name</span>: string;</span><br><span class="line">        <span class="attr">age</span>: number;</span><br><span class="line">        <span class="comment">//定义构造函数</span></span><br><span class="line">        <span class="title function_">constructor</span>(<span class="params">name: string, age: number</span>) &#123;</span><br><span class="line">            <span class="comment">//更新属性</span></span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">name</span> = name;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">age</span> = age;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">get</span> <span class="title function_">nameage</span>() &#123;</span><br><span class="line">            <span class="comment">//获取nameage的时候直接调用</span></span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;调用get&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">name</span> + <span class="string">&quot;_&quot;</span> + <span class="variable language_">this</span>.<span class="property">age</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">set</span> <span class="title function_">nameage</span>(<span class="params">val:string</span>) &#123;</span><br><span class="line">            <span class="comment">//设置nameage的时候直接调用</span></span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;调用set&quot;</span>);</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">name</span> = val.<span class="title function_">split</span>(<span class="string">&quot;_&quot;</span>)[<span class="number">0</span>];</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">age</span> = <span class="title class_">Number</span>(val.<span class="title function_">split</span>(<span class="string">&quot;_&quot;</span>)[<span class="number">1</span>]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">let</span> stu1 = <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;aa&quot;</span>, <span class="number">10</span>);</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(stu1); <span class="comment">//name:&quot;aa&quot;,age:10</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(stu1.<span class="property">nameage</span>); <span class="comment">//aa_10</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>((stu1.<span class="property">nameage</span> = <span class="string">&quot;bb_20&quot;</span>));</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(stu1);<span class="comment">//name:&quot;bb&quot;,age:20</span></span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>
<h4 id="静态成员"><a href="#静态成员" class="headerlink" title="静态成员"></a>静态成员</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 静态成员</span></span><br><span class="line"><span class="comment"> *      通过 static 修饰的属性或方法</span></span><br><span class="line"><span class="comment"> *      调用:类名.xxx 方式调用的，不能通过实例对象来使用</span></span><br><span class="line"><span class="comment"> *      普通方法不能获取静态属性</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">//定义一个基类</span></span><br><span class="line">    <span class="keyword">class</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">        <span class="comment">//定义属性</span></span><br><span class="line">        <span class="keyword">static</span> <span class="attr">name1</span>: string;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//定义构造函数</span></span><br><span class="line">        <span class="title function_">constructor</span>(<span class="params"></span>) &#123;&#125;</span><br><span class="line">        <span class="comment">//定义实例方法</span></span><br><span class="line">        <span class="keyword">static</span> <span class="title function_">sayHi</span>(<span class="params">str: string</span>) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`我是<span class="subst">$&#123;<span class="variable language_">this</span>.name1&#125;</span>`</span>, str);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Person</span>.<span class="property">name1</span>);</span><br><span class="line">    <span class="title class_">Person</span>.<span class="title function_">sayHi</span>(<span class="string">&quot;静态成员方法&quot;</span>);</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>((<span class="title class_">Person</span>.<span class="property">name1</span> = <span class="string">&quot;cc&quot;</span>));</span><br><span class="line">    <span class="title class_">Person</span>.<span class="title function_">sayHi</span>(<span class="string">&quot;静态成员方法&quot;</span>);</span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>

<h4 id="抽象类"><a href="#抽象类" class="headerlink" title="抽象类"></a>抽象类</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 抽象类</span></span><br><span class="line"><span class="comment"> *      抽象方法,只能定义,函数体不能有东西</span></span><br><span class="line"><span class="comment"> *      可以包含非抽象方法</span></span><br><span class="line"><span class="comment"> *      不能被实例化</span></span><br><span class="line"><span class="comment"> *      抽象类的子类必须实现抽象方法和抽象属性才能实例化</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 抽象类类似模板,定义的抽象方法和抽象属性子类必须拥有</span></span><br><span class="line"><span class="comment"> * 抽象类的目的和作用都是为子类服务的</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">//定义一个基类</span></span><br><span class="line">    abstract <span class="keyword">class</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">        <span class="comment">//定义属性</span></span><br><span class="line">        abstract <span class="attr">name</span>: string = <span class="string">&quot;123&quot;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//定义方法</span></span><br><span class="line">        abstract <span class="title function_">eat</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//定义实例方法</span></span><br><span class="line">        <span class="title function_">sayHi</span>(<span class="params">str: string</span>) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`我是<span class="subst">$&#123;<span class="variable language_">this</span>.name&#125;</span>`</span>, str);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">class</span> <span class="title class_">Dog</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Person</span> &#123;</span><br><span class="line">        <span class="attr">name</span>: string;</span><br><span class="line">        <span class="title function_">eat</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;实现eat&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br><span class="line">    <span class="keyword">let</span> dog = <span class="keyword">new</span> <span class="title class_">Dog</span>();</span><br><span class="line">    dog.<span class="title function_">sayHi</span>(<span class="string">&#x27;str&#x27;</span>)</span><br><span class="line">    dog.<span class="title function_">eat</span>()</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(dog.<span class="property">name</span>)</span><br><span class="line">&#125;)();</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h3 id="声明文件"><a href="#声明文件" class="headerlink" title="声明文件"></a>声明文件</h3><blockquote>
<p>当使用第三方插件时需映入声明文件,才能获得代码补全接口提示的功能</p>
</blockquote>
<p>如:jquery  声明文件 @types&#x2F;jquery<br>安装完成之后存放在node_module&#x2F;@type文件夹中，运行项目是ts会自动解析到项目中</p>
<h3 id="内置对象"><a href="#内置对象" class="headerlink" title="内置对象"></a>内置对象</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *  内置对象</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">let</span> <span class="attr">str</span>:string = <span class="string">&quot;str&quot;</span>;</span><br><span class="line">    <span class="keyword">let</span> <span class="attr">str2</span>:string = <span class="keyword">new</span> <span class="title class_">String</span>(<span class="string">&quot;str&quot;</span>) <span class="comment">//“string”是基元，但“String”是包装器对象会报错</span></span><br><span class="line">    <span class="keyword">let</span> <span class="attr">str3</span>:<span class="title class_">String</span> = <span class="keyword">new</span> <span class="title class_">String</span>(<span class="string">&quot;str&quot;</span>) <span class="comment">//正确</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(str,str2,str3)</span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://example.com">lzoxun</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2023/07/22/web/web-script-ts/">http://example.com/2023/07/22/web/web-script-ts/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">学习笔记</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/07/22/web/web-script-es6/" title="ECMAScript"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">ECMAScript</div></div></a></div><div class="next-post pull-right"><a href="/2023/07/22/web/web-script-js/" title="JavaScript 基础"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">JavaScript 基础</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">lzoxun</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">108</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/liaozhongxun"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/liaozhongxun" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:869664233@gmail.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">这是我的文档</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%89%E8%A3%85"><span class="toc-number">1.</span> <span class="toc-text">安装</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%96%E8%AF%91"><span class="toc-number">2.</span> <span class="toc-text">编译</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%89%8B%E5%8A%A8%E7%9B%B4%E6%8E%A5%E7%BC%96%E8%AF%91"><span class="toc-number">2.1.</span> <span class="toc-text">手动直接编译</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#vscode%E8%87%AA%E5%8A%A8%E7%BC%96%E8%AF%91"><span class="toc-number">2.2.</span> <span class="toc-text">vscode自动编译</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ts%E8%AF%AD%E6%B3%95"><span class="toc-number">3.</span> <span class="toc-text">ts语法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E6%B3%A8%E8%A7%A3"><span class="toc-number">3.1.</span> <span class="toc-text">类型注解</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">3.2.</span> <span class="toc-text">基础数据类型</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E5%88%AB%E5%90%8D-Type"><span class="toc-number">3.2.1.</span> <span class="toc-text">类型别名 Type</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E6%8E%A8%E5%AF%BC"><span class="toc-number">3.2.2.</span> <span class="toc-text">类型推导</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E6%96%AD%E8%A8%80"><span class="toc-number">3.2.3.</span> <span class="toc-text">类型断言</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%9D%9E%E7%A9%BA%E7%B1%BB%E5%9E%8B%E6%96%AD%E8%A8%80"><span class="toc-number">3.2.4.</span> <span class="toc-text">非空类型断言 !</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%97%E9%9D%A2%E9%87%8F%E7%B1%BB%E5%9E%8B"><span class="toc-number">3.2.5.</span> <span class="toc-text">字面量类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%B1%BB%E5%9E%8B"><span class="toc-number">3.2.6.</span> <span class="toc-text">函数类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%A4%E5%8F%89%E7%B1%BB%E5%9E%8B"><span class="toc-number">3.2.7.</span> <span class="toc-text">交叉类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E7%BC%A9%E5%B0%8F%EF%BC%88%E7%B1%BB%E5%9E%8B%E4%BF%9D%E6%8A%A4%EF%BC%89"><span class="toc-number">3.2.8.</span> <span class="toc-text">类型缩小（类型保护）</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#interface-%E4%B8%8E-%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.3.</span> <span class="toc-text">interface 与 对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0"><span class="toc-number">3.4.</span> <span class="toc-text">函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%AE%8C%E6%95%B4%E5%86%99%E6%B3%95"><span class="toc-number">3.4.1.</span> <span class="toc-text">函数完整写法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%82%E6%95%B0"><span class="toc-number">3.4.2.</span> <span class="toc-text">参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%82%E6%95%B0%E4%B8%AA%E6%95%B0%E9%97%AE%E9%A2%98"><span class="toc-number">3.4.3.</span> <span class="toc-text">参数个数问题</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E8%B0%83%E7%94%A8%E7%AD%BE%E5%90%8D"><span class="toc-number">3.4.4.</span> <span class="toc-text">函数调用签名</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9E%84%E9%80%A0%E7%AD%BE%E5%90%8D-%E4%BA%86%E8%A7%A3"><span class="toc-number">3.4.5.</span> <span class="toc-text">构造签名(了解)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%AF%E9%80%89-%E9%BB%98%E8%AE%A4-%E5%89%A9%E4%BD%99%E5%8F%82%E6%95%B0"><span class="toc-number">3.4.6.</span> <span class="toc-text">可选&#x2F;默认&#x2F;剩余参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%87%8D%E8%BD%BD-%E4%BA%86%E8%A7%A3"><span class="toc-number">3.4.7.</span> <span class="toc-text">重载(了解)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E4%B8%AD%E7%9A%84this%E7%B1%BB%E5%9E%8B"><span class="toc-number">3.4.8.</span> <span class="toc-text">函数中的this类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0"><span class="toc-number">3.4.9.</span> <span class="toc-text">匿名函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B6%E4%BB%96"><span class="toc-number">3.4.10.</span> <span class="toc-text">其他</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%9B%E5%9E%8B"><span class="toc-number">3.5.</span> <span class="toc-text">泛型</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B3%9B%E5%9E%8B%E7%BA%A6%E6%9D%9F"><span class="toc-number">3.5.1.</span> <span class="toc-text">泛型约束</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB"><span class="toc-number">3.6.</span> <span class="toc-text">类</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%98%E5%8F%96%E5%99%A8"><span class="toc-number">3.6.1.</span> <span class="toc-text">存取器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E6%88%90%E5%91%98"><span class="toc-number">3.6.2.</span> <span class="toc-text">静态成员</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8A%BD%E8%B1%A1%E7%B1%BB"><span class="toc-number">3.6.3.</span> <span class="toc-text">抽象类</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A3%B0%E6%98%8E%E6%96%87%E4%BB%B6"><span class="toc-number">3.7.</span> <span class="toc-text">声明文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.8.</span> <span class="toc-text">内置对象</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/window/win-11/" title="win 11 set">win 11 set</a><time datetime="2023-07-22T07:14:09.700Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/window/win-gitbash/" title="git bash">git bash</a><time datetime="2023-07-22T07:14:09.700Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/window/win-wsl/" title="wsl">wsl</a><time datetime="2023-07-22T07:14:09.700Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/window/win-main/" title="window">window</a><time datetime="2023-07-22T07:14:09.700Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/22/web/%E5%85%BC%E5%AE%B9%E9%97%AE%E9%A2%98/" title="兼容问题">兼容问题</a><time datetime="2023-07-22T07:14:09.699Z" title="发表于 2023-07-22 15:14:09">2023-07-22</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By lzoxun</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/haruto.model.json"},"display":{"position":"right","width":100,"height":200,"right":300},"mobile":{"show":true}});</script></body></html>